home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Brailler 0.5ß / Brailler 0.5ß.source / brlr ƒ / brlr search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  1.0 KB  |  61 lines  |  [TEXT/MMCC]

  1. #include "brlr search.h"
  2.  
  3. short SearchForwards(short startPosition, Str255 theStr, TEHandle hTE)
  4. {
  5.     short            i;
  6.     short            max;
  7.     short            strPos;
  8.     short            strLen;
  9.     unsigned char    *a;
  10.     
  11.     i=startPosition;
  12.     max=(**hTE).teLength;
  13.     HLock((**hTE).hText);
  14.     a=(unsigned char*)(*((**hTE).hText));
  15.     strPos=1;
  16.     strLen=theStr[0];
  17.     while ((i<max) && (strPos<=strLen))
  18.     {
  19.         if (a[i]==theStr[strPos])
  20.             strPos++;
  21.         else
  22.             strPos=1;
  23.         i++;
  24.     }
  25.     
  26.     HUnlock((**hTE).hText);
  27.     if (strPos>strLen)
  28.         return i-strLen;
  29.     else
  30.         return -1;
  31. }
  32.  
  33. short SearchBackwards(short startPosition, Str255 theStr, TEHandle hTE)
  34. /* note that startPosition is the starting position for looking for the last character in theStr */
  35. {
  36.     short            i;
  37.     short            strPos;
  38.     short            strLen;
  39.     unsigned char    *a;
  40.     
  41.     i=startPosition;
  42.     HLock((**hTE).hText);
  43.     a=(unsigned char*)(*((**hTE).hText));
  44.     strLen=theStr[0];
  45.     strPos=strLen;
  46.     while ((i>=0) && (strPos>0))
  47.     {
  48.         if (a[i]==theStr[strPos])
  49.             strPos--;
  50.         else
  51.             strPos=strLen;
  52.         i--;
  53.     }
  54.     
  55.     HUnlock((**hTE).hText);
  56.     if (strPos==0)
  57.         return i+1;
  58.     else
  59.         return -1;
  60. }
  61.